﻿using System.IO;
using Framework.Core;
using Framework.Core.Recorder;
using Rhino.Mocks;
using Xunit;
using MethodFactory = Framework.Core.Reflection.MethodFactory;

namespace Framework.Tests.Core.Recorder
{
    extern alias castlecore;

    public class DependencyRecorderExtensionTest
    {
        private static readonly castlecore::Castle.DynamicProxy.ProxyGenerator ProxyGenerator =
            new castlecore::Castle.DynamicProxy.ProxyGenerator();

        private readonly castlecore::Castle.DynamicProxy.IInterceptor interceptorMock;

        public DependencyRecorderExtensionTest()
        {
            var mockRepository = new MockRepository();
            interceptorMock = mockRepository.StrictMock<castlecore::Castle.DynamicProxy.IInterceptor>();
        }

        [Fact]
        public void Should_throw_exception_when_proxy_is_not_dependency_recorder()
        {
            Assert.Throws<HardMockException>(() => new object().Snapshot(null));
            Assert.Throws<HardMockException>(
                () => ProxyGenerator.CreateInterfaceProxyWithoutTarget<IToto>(interceptorMock).Snapshot(null));
        }

        [Fact]
        public void Should_pack()
        {
            using (var stream = new MemoryStream())
            {
                new DependencyRecorder(
                    new DependencyRepository(new BinarySerializer()), 
                    new CallKeyBuilder(), 
                    new MethodFactory()).Serialize(stream);

                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                var actual = new DependencyReplayer(
                    new DependencyRepository(new BinarySerializer(), stream), 
                    new CallKeyBuilder(), 
                    new MethodFactory());
                Assert.NotNull(actual);
            }
        }

        public interface IToto
        {
        }
    }
}